package com.daqsoft.controller.project;

import com.daqsoft.data.User;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.project.MyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的任务控制器
 *
 * @author 周麟东 on 15:37
 */
@RestController
@RequestMapping("myTask")
public class MyTaskController {

    @Autowired
    private MyTaskService service;

    /**
     * 获取简单任务列表
     *
     * @param role 是负责人还是责任人
     * @return 任务列表
     */
    @RequestMapping("getSimpleTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取简单任务列表")
    public GeneralResultEnum getSimpleTask(HttpServletRequest request, String role) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getSimpleTask(user, role);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }

    @RequestMapping("getFinishTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取已完成的任务")
    public GeneralResultEnum getFinishTask(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getFinishTask(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }
    @RequestMapping("getCheckedTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取已完成的任务")
    public GeneralResultEnum getCheckedTask(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getCheckedTask(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }

    @RequestMapping("getErrorTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取不通过的任务")
    public GeneralResultEnum getErrorTask(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getErrorTask(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }

    @RequestMapping("getChangeTasks")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取变更任务")
    public GeneralResultEnum getChangeTasks(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getChangeTasks(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }

    @RequestMapping("getProjectConfirm")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取项目确认任务")
    public GeneralResultEnum getProjectConfirm(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getProjectConfirm(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }
    @RequestMapping("getCheckTasks")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "获取已经提交的任务")
    public GeneralResultEnum getCheckTasks(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<Task> list = service.getCheckTasks(user);
        return GeneralResultEnum.SUCCESS.setData(formatList(list));
    }


    private List<Map<String, Object>> formatList(List<Task> list) {
        List<Map<String, Object>> tasks = new ArrayList<>();
        for (Task task : list) {
            tasks.add(task.toMap());
        }
        return tasks;
    }

}
